//数据调用函数
function acquire_result(callback, query_type = 1, time_sort, page = 1) {
  $.requestJSON({
    url: Urls.resolve('storage_api_v1:my_dataset'),
    method: 'GET',
    param: {query_type: query_type, time_sort: time_sort, page: page},
    onSuccess(data) {
      console.log(data);
      callback(data);
    }
    //如果获取不成功怎么办
  });
}

function dataset_count(data) {
  $("#dataset-count").text(data.data.count);
}

function dataset_set_table_noValue(data) {
  $("#dataset_result").text(gettext("Nothing......"));
}

function dataset_set_table_hasValue(data) {
  // $("#table-value").val(data.data.datasets[0].filename);
  $("#dataset_result").empty();
  var obj =
    '      <table class="table-content" width="95%" border="0" align="center" cellspacing="0" cellpadding="8">\n' +
    '        <tr class="tr-title"  align="left">\n' +
    '          <td class="td-title-1 first">#</td>\n' +
    '          <td class="td-title-2 second">' + gettext('Title') + '</td>\n' +
    '          <td class="td-title-3 third">' + gettext('Author') + '</td>\n' +
    '          <td class="td-title-4 fourth">' + gettext('Purpose') + '</td>\n' +
    '          <td class="td-title-5 fifth">' + gettext('Number of uses') + '</td>\n' +
    '          <td class="td-title-6 sixth">' + gettext('Options') + '</td>\n' +
    '        </tr>';
  var currentPage = data.data.current_page;
  var numberOfPages = data.data.total_pages;
  var step = (currentPage - 1) * numberOfPages;
  $.each(data.data.results, function () {
    step++;
    var obj2 =
      '        <ul class="list-inline">' +
      '          <li><a role="button" id="" title="' + gettext('Delete') + '"><i class="fa fa-trash-o"></i></a></li>';
    if (this['is_public'])
      obj2 += '<li><a role="button" class="go_to_private" title="' + gettext('Private') + '"><i class="fa fa-eye"></i></a></li></ul>';
    else
      obj2 += '<li><a role="button" class="go_to_public" title="' + gettext('Public') + '"><i class="fa fa-eye-slash"></i></a></li></ul>';
    obj +=
      '        <tr class="tr-body" align="left" id="' + this['id'] + '">\n' +
      '          <td class="td-body-1 first">' + step + '</td>\n' +
      '          <td class="td-body-2 second">' + this['title'] + '</td>\n' +
      '          <td class="td-body-3 third">' + this['author'] + '</td>\n' +
      '          <td class="td-body-6 sixth">' + this['purpose'] + '</td>\n' +
      '          <td class="td-body-7 seventh">' + this['ref_count'] + '</td>\n' +
      '          <td class="td-body-8 eigthth">' + obj2 + '</td>\n' +
      '        </tr>';
  });
  obj += '</table>';
  $("#dataset_result").append(obj);

  $(".tr-body:even").hover(function () {
    $(this).css("background-color", "#DCDCDC");
  }, function () {
    $(this).css("background-color", "#F5F5F5");
  });
  $(".tr-body:odd").hover(function () {
    $(this).css("background-color", "#DCDCDC");
  }, function () {
    $(this).css("background-color", "#FFFFFF");
  });

  $(document).on("click", ".go_to_private", function () {
    $(this).attr({"class": "go_to_public", "title": gettext("公开")});
    $(this).find("i").attr("class", "fa fa-eye-slash");
    obj = $(this).parent().parent().parent().parent();
    // alert(obj.prop('id'));
    // var dataset_id1=obj.attr("id");
  });
  $(document).on("click", ".go_to_public", function () {
    $(this).attr({"class": "go_to_private", "title": gettext("隐藏")});
    $(this).find("i").attr("class", "fa fa-eye");
    obj = $(this).parent().parent().parent().parent();
    // alert(obj.prop('id'));
  });

  $(".tr-body").on('click', function () {
    var dataset_id = $(this).attr("id");
    window.open(Urls.resolve('management:show_data', {dataset_id: dataset_id, query_type: 1}));
  });
}

function dataset_show_all(query_type, time_sort) {
  acquire_result(function (data) {
    dataset_count(data);
    if (data.data.count == 0) {
      dataset_set_table_noValue(data);
    }
    else {
      dataset_set_table_hasValue(data);
      var paginator = $('#dataset_paginator');
      var options = {
        bootstrapMajorVersion: 3,
        currentPage: 1,
        numberOfPages: 5,
        totalPages: data.data.total_pages,
        onPageClicked: function (e, originalEvent, type, page) {
          acquire_result(dataset_set_table_hasValue, query_type, time_sort, page);
        }
      };
      if (options.totalPages > 1) {
        paginator.bootstrapPaginator(options);  // 仅大于一页时分页
      }
    }
  }, query_type, time_sort, page = 1)
}
$(function () {
  dataset_show_all(1, null);

});